package gewu.suleju.controller;

import gewu.suleju.model.pojo.*;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.HouseBaseInforServiceImp;
import gewu.suleju.sevice.imp.HouseCommentServiceImp;
import gewu.suleju.sevice.imp.HouseDetailServiceImp;
import gewu.suleju.sevice.imp.HousePicServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("housedetail")
public class HouseDetailController {
    @Autowired
    HouseDetailServiceImp houseDetailServiceImp;
    @Autowired
    HouseBaseInforServiceImp houseBaseInforServiceImp;
    @Autowired
    HousePicServiceImp housePicServiceImp;
    @Autowired
    HouseCommentServiceImp houseCommentServiceImp;


    @ResponseBody
    @GetMapping("houseDetail")
    public Result checkCode(String id) {
        int houseid=Integer.parseInt(id);
        //  房子详情
        HouseInfor houseInfor=houseDetailServiceImp.findHouseDetailById(houseid);
        //经纪人
        BrokerInfo brokerInfo= houseBaseInforServiceImp.findBrokerInfoByHouseid(houseInfor.getBrokerInfoId());
        //房子基本详情
        HouseBaseInfor houseBaseInfor=houseBaseInforServiceImp.findHouseBaseInforById(houseid);

        //房子照片地址
        List<HousePic> housePics=housePicServiceImp.findHousePicsByHouseInforId(houseid);
        String[] housepics=new String[housePics.size()];
        for (int i = 0; i < housePics.size(); i++) {
            HousePic housePic= housePics.get(i);
            String[] middlehousePi=housePic.getHousePicAddress().split("=");
            if(middlehousePi.length ==1){
                housepics[i]=middlehousePi[0];
            }
            else {
                housepics[i]=middlehousePi[1];
            }
        }
        //房子评论
        List<User> commentUsers=new ArrayList<>();
        List<HouseComment> comments=houseCommentServiceImp.findHouseCommentByHouseInforId(houseid);
        List<User> users=new ArrayList<>(comments.size());
        for (int i = 0; i <comments.size() ; i++) {
            HouseComment comment= comments.get(i);
            SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd");
            System.out.println(sdf.format(comment.getCommentDate()));
//          comment.setCommentDate(new Date(sdf.format(comment.getCommentDate())));
            Integer userid =comment.getUserId();
            User user=houseCommentServiceImp.findUserById( userid);
            commentUsers.add(user);
        }
        //家具设备
        String[] devices=houseBaseInforServiceImp.findKindById(houseBaseInfor.getId());
        //推荐房子
        List<HouseInfor> recommends= houseDetailServiceImp.findALLhouse(houseid);


        // 返回的集合
        HashMap<String,Object> house = new HashMap<String,Object>();
        house.put("houseInfor",houseInfor);
        house.put("houseBaseInfor",houseBaseInfor);
        house.put("housepics",housepics);
        house.put("comments",comments);
        house.put("commentUsers",commentUsers);
        house.put("brokerInfo",brokerInfo);
        house.put("devices",devices);
        house.put("recommends",recommends);

        return new Result("200","查找房子成功",house);
    }
}